﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using System.Data;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class DutyLog_List1 : System.Web.UI.Page
{
    private string DepartmentID;
    int DiaryPion = 34;
    int ParkPion = 7;
    int DutyPion = 10;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            Bind();
            bindChart();
        }

    }

    private void Bind()
    {
        DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        dptDepartment1.DataSource = Yipusoft.Account.BLL.Department.GetList("Right(DepartmentID,3)='000' and DepartmentID<>'1300000'");
        dptDepartment1.DataTextField = "FullName";
        dptDepartment1.DataValueField = "DepartmentID";
        dptDepartment1.DataBind();
        this.dptDepartment1.SelectedValue = DepartmentID.Substring(0, 4) + "000";
        dptDepartment1_SelectedIndexChanged(this, new EventArgs());

        if (!DepartmentID.Substring(2, 3).Equals("000"))
        {
            if (DepartmentID.Substring(4, 1).Equals("0"))
            {
                this.dptDepartment1.Enabled = false;
            }
            else
            {
                if (dptDepartment2.Items.FindByValue(DepartmentID) != null)
                {
                    dptDepartment2.SelectedValue = DepartmentID;
                    this.dptDepartment1.Enabled = false;
                    this.dptDepartment2.Enabled = false;
                }
                else
                {

                    this.dptDepartment1.Enabled = false;
                    if (DepartmentID.Substring(4, 1) == "2")
                    {
                        string ShortName = Yipusoft.Account.BLL.Department.GetDepartment(DepartmentID).ShortName.Substring(0, 2);
                        List<DepartmentInfo> departments = Yipusoft.Account.BLL.Department.GetList(" departmentid like '" + DepartmentID.Substring(0, 4) + "3__' and ShortName like '" + ShortName + "%'  ").ToList();
                        if (departments.Count > 0)
                        {
                            dptDepartment2.Items.Clear();
                            dptDepartment2.DataSource = departments;
                            dptDepartment2.DataBind();
                        }
                        else
                        {
                            dptDepartment2.SelectedValue = DepartmentID;
                            this.dptDepartment1.Enabled = false;
                            this.dptDepartment2.Enabled = false;
                        }

                    }
                }

            }
        }


        for (int i = 2009; i < 2015; i++)
        {
            dptYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        for (int i = 1; i <= 12; i++)
        {
            dptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }

        dptMonth.SelectedValue = DateTime.Now.Month.ToString();
        dptYear.SelectedValue = DateTime.Now.Year.ToString();
        if (Request["Departmentid"] != null)
        {
            dptDepartment1.SelectedValue = Request["Departmentid"].Substring(0, 4) + "000";
            BindDepartment2();
            dptDepartment2.SelectedValue = Request["Departmentid"];
            dptYear.SelectedValue = Request["y"];
            dptMonth.SelectedValue = Request["m"];
        }


    }
    protected void calSchedule_DayRender(object sender, DayRenderEventArgs e)
    {
        DepartmentID = dptDepartment2.SelectedValue;
        CalendarDay d = ((DayRenderEventArgs)e).Day;
        TableCell c = ((DayRenderEventArgs)e).Cell;


        if (d.IsOtherMonth)
        {
            c.Controls.Clear();
        }
        else
        {
            try
            {
                int cuDay = Convert.ToInt32(d.Date.DayOfWeek);
                int temp = (-1) * cuDay;
                DateTime StartTime = DateTime.Parse(d.Date.AddDays(temp).ToString("yyyy-MM-dd"));
                DateTime EndTime = DateTime.Parse(StartTime.AddDays(7).ToString("yyyy-MM-dd"));

                DiarySquadron diary = Diary.GetDiaryByDepartmentIDAndDataTime(DepartmentID, d.Date);

                c.Controls.Add(new LiteralControl("<div style=\"text-align:center; width:100%;height:20px; line-height:22px;font-weight:normal;\">"));
                //c.Controls.Add(aHyperLink);
                if (diary != null)
                {

                    c.Controls.Add(new LiteralControl("<img src='../../../Images/add_small.gif' alt=''/><a href='DiaryDisplay.aspx?id=" + diary.Id + "' title='要事记录'>要事记录</a>"));
                    c.Controls.Add(new LiteralControl("<br/>"));
                }

                if (Duty.GetDutyLog(d.Date, DepartmentID.Trim()) != null)
                {

                    c.Controls.Add(new LiteralControl("<img src='../../../Images/add_small.gif' alt=''/><a href='DutyView.aspx?Date=" + d.Date.ToString("yyyy-MM-dd") + "&DepartmentID=" + DepartmentID + "' title='执勤记录'>执勤记录</a>"));
                    c.Controls.Add(new LiteralControl("<br/>"));
                }

                ParkLogs park = Park.GetParkLog(StartTime, EndTime, DepartmentID);
                if (park != null && park.Date.Value.ToShortDateString() == d.Date.ToShortDateString())
                {
                    c.Controls.Add(new LiteralControl("<img src='../../../Images/add_small.gif' alt=''/><a href='ParkLogView.aspx?Date=" + d.Date.ToString("yyyy-MM-dd") + "&DepartmentID=" + DepartmentID + "' title=''>车场登记</a>"));
                    c.Controls.Add(new LiteralControl("<br/>"));
                    c.Controls.Add(new LiteralControl("</div>"));
                }
                // c.Controls.Add(new TextBox());


                ////显示会议
                //ReportMeeting reportMeeting = new ReportMeeting();
                //List<ReportMeetingInfo> ReportMeetingInfoColletion = reportMeeting.GetList(d.Date) as List<ReportMeetingInfo>;
                //foreach (ReportMeetingInfo info in ReportMeetingInfoColletion)
                //{
                //    string meetingName = info.MeetingName;
                //    if (info.MeetingName.Length > 4)
                //    {
                //        meetingName = info.MeetingName.Substring(0, 4);
                //    }
                //    HyperLink bHyperLink = new HyperLink();
                //    bHyperLink.Text = "☉" + meetingName;
                //    bHyperLink.ToolTip = info.MeetingName;
                //    bHyperLink.NavigateUrl = "MeetingView.aspx?id=" + info.Id;
                //    if (d.Date < DateTime.Now)
                //    {
                //        bHyperLink.ForeColor = System.Drawing.Color.Green;
                //    }
                //    c.Controls.Add(bHyperLink);
                //    c.Controls.Add(new LiteralControl("<br/>"));
                //}
            }
            catch (Exception exc)
            {
                Response.Write(exc.ToString());

            }


        }
    }

    protected void dptDepartment1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartment2();
    }

    private void BindDepartment2()
    {
        dptDepartment2.DataSource = Yipusoft.Account.BLL.Department.GetList("Left(DepartmentID,4)='" + dptDepartment1.SelectedValue.Substring(0, 4) + "' and SubString(DepartmentID,5,1)='3'");
        dptDepartment2.DataTextField = "FullName";
        dptDepartment2.DataValueField = "DepartmentID";
        dptDepartment2.DataBind();
    }
    protected void lbtnQuery_Click(object sender, EventArgs e)
    {
        this.calSchedule.SelectedDate = new DateTime(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth
            .SelectedValue), 1);
        this.calSchedule.VisibleDate = new DateTime(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue), 1);
        bindChart();
        this.UserChart.Titles[0].Text = dptDepartment2.SelectedItem.Text + dptYear.SelectedItem.Text + dptMonth.SelectedItem.Text + "未完成率统计图";

    }
    DiaryDataContext diarydb = new DiaryDataContext();
    DutyLogDataContext dutydb = new DutyLogDataContext();
    ParkLogDataContext parkdb = new ParkLogDataContext();
    void bindChart()
    {
        int year = Convert.ToInt32(dptYear.SelectedValue);
        int month = Convert.ToInt32(dptMonth.SelectedValue);
        int monthDayCount = Account.GetMonthDayCount(year, month);
        int monthFridayCount = Account.GetMonthFridayCount(year, month);
        //总数
        int diaryTotal = DiaryPion * monthDayCount;
        int parkTotal = ParkPion * monthFridayCount;
        int DutyTotal = DutyPion * monthDayCount;
        DepartmentID = dptDepartment2.SelectedValue;
        DepartmentInfo info = Department.GetDepartment(DepartmentID);

        //要事日记
        List<DiarySquadron> diarys = diarydb.DiarySquadron.Where(o => o.DepartmentId == info.DepartmentID && o.DiaryDate.Value.Year == year && o.DiaryDate.Value.Month == month).ToList();
        int diarysum = 0;
        foreach (DiarySquadron diary in diarys)
        {
            diarysum += Account.GetDairyPionScore(diary);
        }

        if (diaryTotal == 0)
        {

            UserChart.Series["查看项目"].Points.AddXY("要事日记", DBNull.Value);
        }
        else
        {
            decimal agv = (decimal)(diaryTotal - diarysum) / (diaryTotal);
            UserChart.Series["查看项目"].Points.AddXY("要事日记", agv);

        }

        // 执勤日录

         List<DutyLogs> dutys = dutydb.DutyLogs.Where(o => o.SetDepartment == info.DepartmentID && o.StartTime.Value.Year == year && o.StartTime.Value.Month == month).ToList();
        int dutysum = 0;
        foreach (DutyLogs duty in dutys)
        {
            dutysum += Account.GetDutyScore(duty);
        }

        if (DutyTotal == 0)
        {

            UserChart.Series["查看项目"].Points.AddXY("执勤日录", DBNull.Value);
        }
        else
        {

            decimal agv = (decimal)(DutyTotal - dutysum) / (DutyTotal);
            UserChart.Series["查看项目"].Points.AddXY("执勤日录", agv);

        }
        //车场日登记
        List<ParkLogs> parks = parkdb.ParkLogs.Where(o => o.DepartmentID == info.DepartmentID && o.Date.Value.Year == year && o.Date.Value.Month == month).ToList();
        int parksum = 0;
        foreach (ParkLogs park in parks)
        {
            parksum += Account.GetParkScore(park);
        }

        if (parkTotal == 0)
        {
            UserChart.Series["查看项目"].Points.AddXY("车场日登记", DBNull.Value);

        }
        else
        {

            decimal agv = (decimal)(parkTotal - parksum) / (parkTotal);
            UserChart.Series["查看项目"].Points.AddXY("车场日登记", agv);

        }


        UserChart.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
        UserChart.Series["查看项目"].LabelFormat = "0.00" + "%";

    }

    protected void calSchedule_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
    {
        dptYear.SelectedValue = e.NewDate.Year.ToString();
        dptMonth.SelectedValue = e.NewDate.Month.ToString();
        lbtnQuery_Click(this, new EventArgs());
        //bindChart();

    }
}
